package org.ohos.rajawali3d.examples.examples;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.render.opengl.EGLConfig;
import ohos.agp.render.opengl.GLES1X;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.multimodalinput.event.TouchEvent;
import ohos.opengl.GLSurfaceProvider;
import org.ohos.rajawali3d.ResourceTable;
import org.rajawali3d.renderer.ISurfaceRenderer;
import org.rajawali3d.renderer.Renderer;
import org.rajawali3d.view.IDisplay;
import org.rajawali3d.view.SurfaceView;

public abstract class AExampleFragment2 extends Fraction implements IDisplay, Component.ClickedListener {
    public static final String BUNDLE_EXAMPLE_URL = "BUNDLE_EXAMPLE_URL";
    protected SurfaceView mRenderSurface;
    protected ISurfaceRenderer mRenderer;
    protected ProgressBar mProgressBarLoader;
    protected DirectionalLayout mLayout;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }

    public Component onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        return null;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        super.onComponentAttached(scatter, container, intent);
        mLayout = (DirectionalLayout) scatter.parse(ResourceTable.Layout_relative_layout_loader_container, container, true);
        Component component = mLayout.findComponentById(ResourceTable.Id_rajwali_surface); //bring to front.
        component.getComponentParent().moveChildToFront(component);
        mRenderSurface = new org.rajawali3d.view.SurfaceView(this);
        mRenderSurface.pinToZTop(true);
        mLayout.addComponent(mRenderSurface);
        onCreateView(scatter, container, intent);
        mRenderer = createRenderer();
        onBeforeApplyRenderer();
        applyRenderer();
        return mLayout;
    }

    protected void onBeforeApplyRenderer() {
        mRenderSurface.setFrameRate(60);
        mRenderSurface.setTransparent(true);
        mRenderSurface.setRenderMode(GLSurfaceProvider.RENDERMODE_CONTINUOUSLY);
    }

    protected void applyRenderer() {
        mRenderSurface.setEGLContextClientVersion(3);
        mRenderSurface.setSurfaceRenderer(mRenderer);
    }

    @Override
    public void onClick(Component component) {
    }

    @Override
    protected void onActive() {
        super.onActive();
        mRenderSurface.onResume();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        mRenderSurface.onPause();
    }

    protected void hideLoader() {
        new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
            @Override
            public void run() {
                mProgressBarLoader.setVisibility(Component.HIDE);
            }
        });
    }

    protected void showLoader() {
        new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
            @Override
            public void run() {
                mProgressBarLoader.setVisibility(Component.VISIBLE);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
    }


    protected static abstract class AExampleRenderer extends Renderer {

        final AExampleFragment2 exampleFragment;

        public AExampleRenderer(Context context, AExampleFragment2 fragment) {
            super(context);
            exampleFragment = fragment;
        }

        @Override
        public void onRenderSurfaceCreated(EGLConfig config, GLES1X gl, int width, int height) {
            super.onRenderSurfaceCreated(config, gl, width, height);
        }

        @Override
        public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {

        }

        @Override
        public void onTouchEvent(TouchEvent event) {
        }

    }
}
